package com.nj.liandi.gulimall.order.listener;

import com.nj.liandi.common.to.mq.SeckillOrderTo;
import com.nj.liandi.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * description: 监听秒杀服务消息,创建订单
 * date: 2021/11/11 16:44
 * author: maizj
 * version: 1.0
 */
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSeckillListener {

    public static final Logger log = LoggerFactory.getLogger(OrderSeckillListener.class);

    @Autowired
    OrderService orderService;

    /** description: 定时关闭订单
     * version: 1.0
     * date: 2021/11/4 14:16
     * */
    @RabbitHandler
    public void listener(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws IOException {
        log.info("准备创建秒杀单的详细信息......");
        try{
            orderService.createSeckillOrder(seckillOrderTo);
            //手动调用支付宝收单功能
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
